<?php
namespace app\index\controller;
use think\Controller;
use think\request;
use think\Db;
use think\facade\Log;
use app\service\apiService;
use app\service\invoiceService;
use app\index\controller\Baseali;
class Gatepay extends Baseali{
    public function index(){
        $gateid=request()->get("gateid");
        $gateinfo=Db::table("parking_gatepay")->where("id",$gateid)->find();
        $this->assign("gateinfo",$gateinfo);

        $parkinginfo=Db::table("parkinglotinfo")->where("parkingNo",$gateinfo["parkingNo"])->find();
        $communityinfo=Db::table("community")->where("id",$parkinginfo["community_id"])->find();
        $merchant_name = Db::name('property')->where('id',$communityinfo["property_id"])->value('merchant_name');
        $this->assign("merchant_name",$merchant_name);
        return $this->fetch();
	    
        
    }
    public function gatepay(){
        $gateid=request()->post("gateid");
	    $gateinfo=Db::table("parking_gatepay")->where("id",$gateid)->find();//$gateinfo["parkingNo"]
	    $parkinginfo=Db::table("parkinglotinfo")->where("parkingNo",$gateinfo["parkingNo"])->find();
	    $communityinfo=Db::table("community")->where("id",$parkinginfo["community_id"])->find();
	    $invoiceinfo=(new invoiceService())->parkingInvoice($gateinfo["parkingNo"]);;
        $info["gateid"]=$gateinfo["id"];
        $info["parkingNo"]=$parkinginfo["parkingNo"];
        $info["parkingName"]=$parkinginfo["parkingName"];
       
        $info["gateName"]=$gateinfo["gateName"];
		$info["user_id"]=session("user_id");
        $info["order_money"]=request()->post("pay_money");
        $info["remark"]=request()->post("remark");
		$info["user_id"]=session("user_id");
		
		$info["pay_money"]=request()->post("pay_money");
		$info["out_trade_no"]=date('YmdHiss').rand(100000,999999);
        $info["subject"] =$gateinfo["gateName"];
		$token = Db::name('property')->where('id',$communityinfo["property_id"])->value('accredit');;
        $api = new apiService();
        $notifyUrl="https://www.aliwgzf.com/index/callback/gatepayNotice";
       
        
        $is_support_invoice="false";
        if($invoiceinfo["is_support_invoice"]=="1"){
            $is_support_invoice="true";
        }
        
        
	    $invoice_merchant_name=$invoiceinfo["invoice_merchant_name"];
	    $tax_num=$invoiceinfo["taxrate"];
        $result = $api->gatepayCreate(json_encode(['out_trade_no'=> $info["out_trade_no"],'total_amount'=>$info["pay_money"],'buyer_id'=>$info["user_id"],'subject' =>$info["subject"],'token'=>$token ,'notifyurl'=>$notifyUrl,'is_support_invoice'=>$is_support_invoice,'invoice_merchant_name'=>$invoice_merchant_name,'tax_num'=>$tax_num]));
        
        if(property_exists($result,'sub_msg')){	
			return json(['info' => $result->sub_msg]);
		}else{
			$info['trade_no'] =  $result->trade_no;
			$res=Db::table("parkgatebill")->insert($info);
			//返回结果到前台唤起收银台
			if($res){
				return json(['order_no' => $info['trade_no']]);
			}else{
				return json(['info' => '订单存储失败']);
			}
		}	
    }
}
